﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Data.Objects.SqlClient;
using HXDNB.Infrastructure.Helper;

namespace HXDNB.DAL
{
    public class ClassBLL : BaseDAL<Class>
    {
        public ClassBLL() : base("Class") { }

        /// <summary>
        /// 查询产品列表
        /// </summary>
        public IQueryable<ClassList> GetList(int ParentID = -1)
        {
            var list = from i in ClassList
                       where ParentID < 0 || i.ParentID == ParentID
                       select i;
            return list.OrderBy(i => i.ID).AsQueryable();
        }
        /// <summary>
        /// 查询下拉菜单产品列表
        /// </summary>
        public List<SelectListItem> GetSelects(SelectListItem frist = null)
        {
            List<SelectListItem> ListItem = new List<SelectListItem>();
            GetSelects(0, ref ListItem);
            if (frist != null)
            {
                ListItem.Insert(0, frist);
            }
            return ListItem;
        }

        #region 私有方法


        private void GetSelects(int ParentID, ref List<SelectListItem> ListItem)
        {
            var list = GetList(ParentID);
            foreach (var item in list)
            {
                ListItem.Add(new SelectListItem() { Text = (item.ParentName == null ? "" : (item.ParentName + " > ")) + item.Name, Value = item.ID.ToString() });
                GetSelects(item.ID, ref ListItem);
            }
        }


        private List<ClassList> ClassList
        {
            get
            {
                List<ClassList> list = new List<ClassList>();
                object obj = CacheHelp.GetCache("GetSelects");
                if (obj == null)
                {

                    list = (from i in _e.Class
                            join j in _e.Class on i.ParentID equals j.ID into Temp
                            from t in Temp.DefaultIfEmpty()
                            select new ClassList() { ID = i.ID, Name = i.Name, ParentID = i.ParentID, Remarks = i.Remarks, ParentName = t.Name }).ToList();
                    CacheHelp.SetCache("GetSelects", list);
                }
                else
                {
                    list = (List<ClassList>)obj;
                }
                return list;

            }
        }
        #endregion
    }
}
